<template>
  <div class='xtx-goods-page' v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/" >首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{goods.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem to="/">{{goods.categories[0].name}}</XtxBreadItem>
        <!-- `/category/sub/${goods.categories[0].id}` -->
        <XtxBreadItem>{{goods.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
          <!-- 图片预览区 -->
            <div class="media">
                <XtxImageView :image-list="goods.mainPictures" />
                <!-- 统计数量 -->
                <GoodsSale :goods='goods'/>
            </div>
            <!-- 商品信息区 -->
            <div class="spec">
                <!-- 商品信息区 -->
                <GoodsInfo :goods='goods'/>
                <!-- 规格选择组件 -->
                <XtxGoodSku :goods="goods" @change="goodSkuChangeFn"  />
                <!-- 数量选择 -->
                <XtxNumbox v-model="count" :min='1' :max='goods.inventory'/>
                <!-- 添加购物车 -->
                <XtxButton @click="addToCart()" type="primary" style="margin-top: 20px">加入购物车</XtxButton>
            </div>
        </div>

      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <!-- 商品详情 -->
          <GoodsDetail :goods="goods"/>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goods.id" :type="1" />
          <GoodsHot :goodsId="goods.id" :type="2" />
          <GoodsHot :goodsId="goods.id" :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reqFindGoods } from '@/api/goods'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
import GoodsSale from './components/goods-sale.vue'
import GoodsInfo from './components/goods-info.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import { useStore } from 'vuex'
import { message } from '@/components/Message'
export default {
  name: 'XtxGoodsPage',
  components: { GoodsSale, GoodsInfo, GoodsDetail, GoodsHot },
  setup () {
    const route = useRoute()
    const goods = ref(null)
    const store = useStore()
    reqFindGoods(route.params.id).then(({ result }) => {
      console.log(result)
      goods.value = result
    })

    // 获取到的是用户点击选择规格里的信息 使值更新变化到页面
    const goodSkuChangeFn = (val) => {
      // console.log(val)
      if (val.skuId) {
        for (const key in val) {
          goods.value[key] = val[key]
        }
      }
    }

    // 数量绑定
    const count = ref(1)

    // 添加购物车
    const addToCart = () => {
      // 规格必须
      if (!goods.value.skuId) return message({ text: '请选择商品规格' })

      // 数量大于库存
      if (count.value > goods.value.inventory) return message({ text: '库存超出' })

      const goodsObj = {
        id: goods.value.id, // 商品id
        name: goods.value.name, // 商品名称
        picture: goods.value.mainPictures[0], // 商品图片
        skuId: goods.value.skuId, // 有效skuId
        price: goods.value.oldPrice, // 原价格
        nowPrice: goods.value.price, // 现价格
        attrsText: goods.value.specsText, // 规格文案
        stock: goods.value.inventory, // 库存
        isEffective: true, // 是否有效商品
        selected: true, // 是否选中
        count: count.value // 商品数量
      }
      // console.log(goodsObj)

      store.dispatch('cart/asyncInertCart', goodsObj).then(() => {
        message({ type: 'success', text: '添加购物车成功' })
        goods.value.inventory -= goods.value.count
      })

      // alert('加入购物车')
    }

    return { goods, goodSkuChangeFn, count, addToCart }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
  .goods-info {
      min-height: 600px;
      background: #fff;
      display: flex;
      .media {
        width: 580px;
        height: 600px;
        padding: 30px 50px;
      }
      .spec {
        flex: 1;
        padding: 30px 30px 30px 0;
      }
    }
</style>
